// 存放交互逻辑JS
// javascript 模块化

var seckill = {
    //封装AJAX的URL
    URL: {
        now:function () {
            // 获取时间url封装
            return '/seckill/time/now';
        },

        exposer: function(seckillId){
            return '/seckill/' + seckillId + '/exposer';
        },

        execution:function (seckillId,md5) {
            return '/seckill/' + seckillId + '/' + md5 + '/execution';
        }

    },
    //秒杀逻辑 实现
    handleSeckillKill:function(seckillId,node){
        node.hide().html('<button class="btn btn-primary btn-lg" id = "killBtn">开始秒杀</button>');
        $.post(seckill.URL.exposer(seckillId),{},function (resulet) {
            if(resulet && resulet['success']){
                var exposer = resulet['data'];
                if(exposer['exposed']){
                    //开启秒杀
                    node.show();
                    var md5 = exposer['md5'];
                    var killUrl = seckill.URL.execution(seckillId,md5);
                    //one 绑定一次点击时间
                    $('#killBtn').one('click',function () {
                        //执行秒杀操作请求
                        $(this).addClass('disabled');
                        //发送秒杀请求 
                        $.post(killUrl,{},function (result) {
                            if(result && result['success']){
                                var killResult = result['data'];
                                var state = killResult['state'];
                                var stateInfo = killResult['stateInfo'];
                                //显示秒杀结果
                                node.html('<span class = "label label-success">'+stateInfo+'</span>');
                            }else{
                                console.log(result)
                            }
                        });
                    });
                }else{
                    //秒杀未开始
                    var newTime = resulet['newTime'];
                    var startTime  =  resulet['startTime'];
                    var endTime = resulet['endTime'];
                    seckill.countdown(seckillId,newTime,startTime,endTime);
                }
            }else{
                console.log(resulet);
            }
        });
    },
    countdown:function (seckillId,nowTime,startTime,endTime) {
        var seckillBox =  $("#seckill-box");
        if(nowTime > endTime){
            seckillBox.html("秒杀已经结束!");
        }else if(nowTime < startTime){
            //计时时间绑定
            var killTime = new Date(startTime + 1000);
            seckillBox.countdown(killTime,function (event) {
                var format = event.strftime('秒杀倒计时: %D天 %H时 %M分 %S秒');
                seckillBox.html(format);
                //倒计时完成之后,回调函数
            }).on('finish.countdown',function () {
                //获取秒杀地址,控制实逻辑,出现秒杀按钮
                seckill.handleSeckillKill(seckillId,seckillBox);
                
            });
        }else{
            seckill.handleSeckillKill(seckillId,seckillBox);
            // seckillBox.html("秒杀开始");
        }
    },
    // 验证手机号码
    validataPhone: function (phone) {
        //isNaN();是否是纯数字
        if (phone && phone.length == 11 && !isNaN(phone)) {
            return true;
        } else {
            return false;
        }
    },
    //封装详情页
    detail: {
        //初始化详情页
        init: function (params) {
            //用户手机验证相关,计时交互
            // 直接访问cookie JS cookie 插件实现
            var keyPhone = $.cookie('killPhone');


            //验证手机号码
            if (!seckill.validataPhone(keyPhone)) {
                //model 对象
                var keyPhoneModel = $("#killPhoneModel");
                keyPhoneModel.modal({
                    show: true,
                    backdrop: 'static',
                    keyboard: false,
                });
                $("#killPhoneBtn").click(function () {
                    var inputPhone = $("#killPhoneKey").val();
                    if (seckill.validataPhone(inputPhone)) {
                        //电话写入cookie
                        $.cookie('killPhone', inputPhone, {expires: 7, path: '/seckill'});
                        //刷新页面
                        window.location.reload();
                    } else {
                        $("#killPhoneMessage").hide().html('<label class="label label-danger">手机号码填写有误</label> ').show(300);
                    }
                });
            }
            //如果已经登录
            // js 访问 json 数据的方式
            var startTime = params['startTime'];
            var endTime = params['endTime'];
            var seckillId = params['seckillId'];
            $.get(seckill.URL.now(),{},function (result) {
                if(result && result['success']){
                    var nowTime = result['data'];
                    seckill.countdown(seckillId,nowTime,startTime,endTime);
                }else{
                    console.log(result);
                }
            });
        }
    }
}
